

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;


    public class StudentSystem {
        private static final String FILE_PATH = "student_scores.txt"; // 文件路径
        private List<Student> students = new ArrayList<>(); // 存储学生的列表

        /**
         * 构造函数，在创建对象时从文件加载数据
         */
        public StudentSystem() {
            loadDataFromFile();
        }

        /**
         * 从文件加载学生数据
         */
        private void loadDataFromFile() {
            try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
                String line;
                while ((line = reader.readLine())!= null) {
                    String[] parts = line.split(",");
                    if (parts.length == 7) {
                        String className = parts[0];
                        String studentId = parts[1];
                        String name = parts[2];
                        String gender = parts[3];
                        double javaScore = Double.parseDouble(parts[4]);
                        double webDesignScore = Double.parseDouble(parts[5]);
                        double englishScore = Double.parseDouble(parts[6]);
                        students.add(new Student(className, studentId, name, gender, javaScore, webDesignScore, englishScore));
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * 将学生数据保存到文件
         */
        private void saveDataToFile() {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
                for (Student student : students) {
                    writer.write(student.getClassName() + "," + student.getStudentId() + "," + student.getName() + "," + student.getGender() + "," + student.getJavaScore() + "," + student.getWebDesignScore() + "," + student.getEnglishScore());
                    writer.newLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         * 显示所有学生的成绩，并按照学号排序
         */
        public void displayAllStudents() {
            Collections.sort(students, Comparator.comparing(Student::getStudentId));
            for (Student student : students) {
                System.out.println(student);
            }
        }

        /**
         * 添加学生成绩，初始成绩为 0
         */
        public void addStudent() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入班级：");
            String className = scanner.nextLine();
            System.out.print("请输入学号：");
            String studentId = scanner.nextLine();
            System.out.print("请输入姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入性别：");
            String gender = scanner.nextLine();

            students.add(new Student(className, studentId, name, gender, 0, 0, 0));
            saveDataToFile();
            System.out.println("添加成功！");
        }

        /**
         * 根据学号修改学生成绩
         */
        public void modifyStudent() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要修改的学号：");
            String studentId = scanner.nextLine();

            Student studentToModify = findStudentById(studentId);
            if (studentToModify!= null) {
                System.out.print("请输入新的 Java 成绩：");
                double javaScore = scanner.nextDouble();
                System.out.print("请输入新的网页设计成绩：");
                double webDesignScore = scanner.nextDouble();
                System.out.print("请输入新的英语成绩：");
                double englishScore = scanner.nextDouble();

                studentToModify.setJavaScore(javaScore);
                studentToModify.setWebDesignScore(webDesignScore);
                studentToModify.setEnglishScore(englishScore);
                saveDataToFile();
                System.out.println("修改成功！");
            } else {
                System.out.println("未找到该学号的学生！");
            }
        }

        /**
         * 根据学号删除学生成绩
         */
        public void deleteStudent() {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入要删除的学号：");
            String studentId = scanner.nextLine();

            Student studentToDelete = findStudentById(studentId);
            if (studentToDelete!= null) {
                students.remove(studentToDelete);
                saveDataToFile();
                System.out.println("删除成功！");
            } else {
                System.out.println("未找到该学号的学生！");
            }
        }

        /**
         * 计算每个学生的总成绩
         */
        public void calculateTotalScoreForEachStudent() {
            for (Student student : students) {
                System.out.println(student.getName() + " 的总成绩为：" + student.getTotalScore());
            }
        }

        /**
         * 计算每个学生的平均成绩
         */
        public void calculateAverageScoreForEachStudent() {
            for (Student student : students) {
                System.out.println(student.getName() + " 的平均分为：" + student.getAverageScore());
            }
        }

        /**
         * 根据学号查找学生
         *
         * @param studentId 学号
         * @return 找到的学生对象，如果未找到则返回 null
         */
        public Student findStudentById(String studentId) {
            for (Student student : students) {
                if (student.getStudentId().equals(studentId)) {
                    return student;
                }
            }
            return null;
        }

        /**
         * 根据姓名查找学生
         *找到的学生对象，如果未找到则返回 null
         */
        public Student findStudentByName(String name) {
            for (Student student : students) {
                if (student.getName().equals(name)) {
                    return student;
                }
            }
            return null;
        }

        /**
         * 按照总成绩降序排序并显示
         */
        public void sortByTotalScore() {
            Collections.sort(students, Comparator.comparing(Student::getTotalScore).reversed());
            for (Student student : students) {
                System.out.println(student);
            }
        }

        /**
         * 按照 Java 成绩降序排序并显示
         */
        public void sortByJavaScore() {
            Collections.sort(students, Comparator.comparing(Student::getJavaScore).reversed());
            for (Student student : students) {
                System.out.println(student);
            }
        }

        /**
         * 按照网页设计成绩降序排序并显示
         */
        public void sortByWebDesignScore() {
            Collections.sort(students, Comparator.comparing(Student::getWebDesignScore).reversed());
            for (Student student : students) {
                System.out.println(student);
            }
        }

        /**
         * 按照英语成绩降序排序并显示
         */
        public void sortByEnglishScore() {
            Collections.sort(students, Comparator.comparing(Student::getEnglishScore).reversed());
            for (Student student : students) {
                System.out.println(student);
            }
        }

        /**
         * 主函数，程序入口
         */
        public static void main(String[] args) {
            StudentSystem system = new StudentSystem();
            Scanner scanner = new Scanner(System.in);
            System.out.println("------------------------------------------------------欢迎来到我的学生成绩管理系统------------------------------------------------------------------------------");
            System.out.println("                                                                                          ");

            while (true) {
                System.out.println("----------------------------------------------------1. 显示所有学生的成绩-----------------------------------------------");
                System.out.println("----------------------------------------------------2. 添加学生----------------------------------------------------");
                System.out.println("----------------------------------------------------3. 修改或者添加学生成绩----------------------------------------------------");
                System.out.println("----------------------------------------------------4. 删除学生成绩--------------------------------------------------------");
                System.out.println("----------------------------------------------------5. 计算统计每个学生的总成绩---------------------------------------------");
                System.out.println("----------------------------------------------------6. 计算统计每个学生的平均分-----------------------------------------------");
                System.out.println("----------------------------------------------------7. 按学号查找学生---------------------------------------------------");
                System.out.println("----------------------------------------------------8. 按姓名查找学生-----------------------------------------------------");
                System.out.println("----------------------------------------------------9. 按总成绩排序-----------------------------------------------------");
                System.out.println("---------------------------------------------------10. 按照Java 分数排序-----------------------------------------------");
                System.out.println("---------------------------------------------------11. 按照网页设计分数排序-----------------------------------------------");
                System.out.println("---------------------------------------------------12. 按照英语分数排序---------------------------------------------------------");
                System.out.println("---------------------------------------------------13. 退出系统-------------------------------------------------------");
                System.out.print("请选择操作（数字1到13）：");

                int choice = scanner.nextInt();
                scanner.nextLine();

                switch (choice) {
                    case 1:
                        system.displayAllStudents();
                        break;
                    case 2:
                        system.addStudent();
                        break;
                    case 3:
                        system.modifyStudent();
                        break;
                    case 4:
                        system.deleteStudent();
                        break;
                    case 5:
                        system.calculateTotalScoreForEachStudent();
                        break;
                    case 6:
                        system.calculateAverageScoreForEachStudent();
                        break;
                    case 7:
                        System.out.print("请输入学号：");
                        String studentId = scanner.nextLine();
                        Student student = system.findStudentById(studentId);
                        if (student!= null) {
                            System.out.println(student);
                        } else {
                            System.out.println("未找到该学号的学生！");
                        }
                        break;
                    case 8:
                        System.out.print("请输入姓名：");
                        String name = scanner.nextLine();
                        Student studentByName = system.findStudentByName(name);
                        if (studentByName!= null) {
                            System.out.println(studentByName);
                        } else {
                            System.out.println("未找到该姓名的学生！");
                        }
                        break;
                    case 9:
                        system.sortByTotalScore();
                        break;
                    case 10:
                        system.sortByJavaScore();
                        break;
                    case 11:
                        system.sortByWebDesignScore();
                        break;
                    case 12:
                        system.sortByEnglishScore();
                        break;
                    case 13:
                        System.out.println("退出系统！");
                        return;
                    default:
                        System.out.println("无效的选择，请重新输入！");
                }
            }
        }
    }


